package com.lv.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lv.data.Result;
import com.lv.model.TbItem;
import com.lv.service.GoodsService;

import com.lv.vo.GoVo;

import com.lv.vo.GoodsVo;
import com.lv.vo.ItemVo;
import com.lv.vo.TemVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("goods")
public class GoodsControoler {

    @Resource
    private GoodsService gs;

    //增加商品
    @PostMapping
    public Result add( @RequestBody GoVo vo){
        List<TbItem> title = JSONObject.parseArray(JSON.toJSONString(vo.getTitle()), TbItem.class);
        List<TbItem> l = JSONObject.parseArray(JSON.toJSONString(vo.getItems()), TbItem.class);
        gs.add(vo,title,l);
        return Result.OK();
    }

    //商品详情查询
    @PostMapping("detail")
    public Result getGoodsDetail(@RequestParam Long goodsId){
        GoodsVo vo = gs.getGoodsDetail(goodsId);
        return Result.OK().setData(vo);
    }

    //增加购物车 根据商品Id去查询商品的数据
    @GetMapping("getItem")
    public Result getItem(@RequestParam Long itemId){
        TemVo vo = gs.getItem(itemId);
        return Result.OK().setData(vo);
    }



}
